package com.ruoyi.web.controller.lucky;

import java.util.List;
import java.util.Objects;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lucky.domain.TBlindbox;
import com.ruoyi.lucky.service.ITBlindboxService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 盲盒管理Controller
 * 
 * @author ruoyi
 * @date 2020-11-12
 */
@Controller
@RequestMapping("/lucky/blindbox")
public class TBlindboxController extends BaseController
{
    private String prefix = "lucky/blindbox";

    @Autowired
    private ITBlindboxService tBlindboxService;

    @RequiresPermissions("lucky:blindbox:view")
    @GetMapping()
    public String blindbox()
    {
        return prefix + "/blindbox";
    }

    /**
     * 查询盲盒管理列表
     */
    @RequiresPermissions("lucky:blindbox:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TBlindbox tBlindbox)
    {
        startPage();
        List<TBlindbox> list = tBlindboxService.selectTBlindboxList(tBlindbox);
        return getDataTable(list);
    }

    /**
     * 导出盲盒管理列表
     */
    @RequiresPermissions("lucky:blindbox:export")
    @Log(title = "盲盒管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TBlindbox tBlindbox)
    {
        List<TBlindbox> list = tBlindboxService.selectTBlindboxList(tBlindbox);
        ExcelUtil<TBlindbox> util = new ExcelUtil<TBlindbox>(TBlindbox.class);
        return util.exportExcel(list, "blindbox");
    }

    /**
     * 新增盲盒管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存盲盒管理
     */
    @RequiresPermissions("lucky:blindbox:add")
    @Log(title = "盲盒管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TBlindbox tBlindbox)
    {
        //库存数量等于上架数量
        tBlindbox.setStockcount(tBlindbox.getOncount());
        return toAjax(tBlindboxService.insertTBlindbox(tBlindbox));
    }

    /**
     * 修改盲盒管理
     */
    @GetMapping("/edit/{blindboxId}")
    public String edit(@PathVariable("blindboxId") Long blindboxId, ModelMap mmap)
    {
        TBlindbox tBlindbox = tBlindboxService.selectTBlindboxById(blindboxId);
        mmap.put("tBlindbox", tBlindbox);
        return prefix + "/edit";
    }

    /**
     * 修改保存盲盒管理
     */
    @RequiresPermissions("lucky:blindbox:edit")
    @Log(title = "盲盒管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TBlindbox tBlindbox)
    {
        return toAjax(tBlindboxService.updateTBlindbox(tBlindbox));
    }

    /**
     * 删除盲盒管理
     */
    @RequiresPermissions("lucky:blindbox:remove")
    @Log(title = "盲盒管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tBlindboxService.deleteTBlindboxByIds(ids));
    }

    /**
     * 盲盒状态修改
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("lucky:blindbox:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(TBlindbox tBlindbox)
    {
        return toAjax(tBlindboxService.changeStatus(tBlindbox));
    }

    /**
     * 商品管理页面
     */
    @GetMapping("/setGoods")
    public String setGoods(ModelMap mmap, Long blindboxId) {
        mmap.put("blindboxId", blindboxId);
        return "lucky/goods/goods";
    }


}
